package com.ayoka.parenting.ui.meditation.detail;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.MeditationBean;
import com.ayoka.parenting.bean.MeditationDetailBean;
import com.ayoka.parenting.databinding.ActivityMeditationDetailBinding;
import com.ayoka.parenting.mvp.MVPBaseActivity;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.StatusBarUtils;

import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: MeditationDetailActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class MeditationDetailActivity extends MVPBaseActivity<ActivityMeditationDetailBinding, MeditationDetailContract.View, MeditationDetailPresenter> implements MeditationDetailContract.View {

    public static void start(Activity activity, String meditationId, String title, int mediaType) {
        Intent intent = new Intent(activity, MeditationDetailActivity.class);
        intent.putExtra("MeditationId", meditationId);
        intent.putExtra("Title", title);
        intent.putExtra("MediaType", mediaType);
        activity.startActivity(intent);
    }

    private String meditationId;
    private String title;
    private int mediaType;

    private MeditationDetailBean meditationDetailBean;

    private int currentIndex;

    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            meditationId = intent.getStringExtra("MeditationId");
            title = intent.getStringExtra("Title");
            mediaType = intent.getIntExtra("MediaType", 0);
        }
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_meditation_detail;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        if (mediaType == 0) {
            ToolBarOptions options = new ToolBarOptions();
            options.navigateId = R.mipmap.icon_back_white;
            options.backgroundResource = 0;
            setCustomToolBar(mDataBinding.customToolbar.toolbar, options);
        } else {
            ToolBarOptions options = new ToolBarOptions(title);
            setCustomToolBar(mDataBinding.customToolbar.toolbar, options);
        }

        if (mPresenter != null) {
            mPresenter.getMeditationDetailData(meditationId);
        }
    }

    @Override
    protected MeditationDetailPresenter getPresenter() {
        return new MeditationDetailPresenter();
    }


    @Override
    public void configMeditationDetailData(MeditationDetailBean data) {
        if (data == null && data.meditation == null) return;
        this.meditationDetailBean = data;
        Fragment fragment;
        if (meditationDetailBean.meditation.MediaType == 1) {
            fragment = MeditationDetailVideoTypeFragment.newInstance(meditationDetailBean);
            StatusBarUtils.setStatusTextColor(this, true);
        } else {
            fragment = MeditationDetailAudioTypeFragment.newInstance(meditationDetailBean);
            StatusBarUtils.setStatusTextColor(this, false);
        }
        configFragment(fragment);
    }

    @Override
    public void onDialogDismiss(DialogFragment dialog) {

    }

    @Override
    public void onSelectedMeditationAudio(MeditationBean.MeditationVoiceBean value) {

    }

    @Override
    public void onSelectedMeditationBackgroundMusic(MeditationDetailBean.BgMusicsBean value) {

    }

    @Override
    public void onChangeMeditationBackgroundVolume(float value) {

    }

    @Override
    public void onUpdateCompletedStatusCallback(boolean isSuccess) {

    }


    public void configFragment(Fragment fragment) {
        if (fragment == null) return;
        try {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.frameLayout, fragment)
                    .commitAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
